<template>
  <div class="common-page f-c">
    <el-form inline>
      <el-form-item label="课程名称">
        <el-select v-model="searchParams.course_id" clearable>
          <el-option v-for="item in courseList" :key="item.id" :value="item.id" :label="item.course_name" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="margin-left: 15px" @click="onSearch">搜索</el-button>
        <el-button type="primary" style="margin-left: 15px" @click="onReset">清除</el-button>
      </el-form-item>
    </el-form>
    <div class="table-box">
      <el-table
        v-loading="loading"
        style="width: 100%"
        height="100%"
        stripe
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column type="index" align="center" label="序号" />
        <el-table-column
          v-for="item in columns"
          :key="item.prop"
          :prop="item.prop"
          :label="item.label"
          align="center"
        />
        <el-table-column label="操作" fixed="right" width="120px">
          <template slot-scope="scope">
            <div  v-if="scope.row.CourseStudents.length < scope.row.total">
              <el-button style="color: #0DB493" type="text" v-if="scope.row.CourseStudents.find(el => el.student_id === userInfo.id)">
                已选
              </el-button>
              <el-button
                v-else
                type="text"
                @click="onEdit(scope.row)"
              > 选课
              </el-button>
            </div>

            <el-button v-else style="color: red" type="text" >
              已满
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <PageAdd ref="PageAddRef" />
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import { http } from '@/api'
import { mapGetters } from 'vuex'

export default {
  name: 'Page',
  computed: {
    ...mapGetters(['categoryList', 'userInfo'])
  },
  mixins: [crud],
  data() {
    return {
      request: http.course_start,
      deleteKey: 'id',
      searchParams: {
        course_id: ''
      },
      columns: [
        { prop: 'category.name', label: '课程类型' },
        { prop: 'course.course_name', label: '课程名称' },
        { prop: 'teacher.teacher_name', label: '老师姓名' },
        { prop: 'count', label: '已选人数' },
        { prop: 'total', label: '总人数' },
        { prop: 'created_at', label: '创建时间' }
      ],
      courseList: []
    }
  },
  created() {
    http.course.list().then(res => {
      this.courseList = res
    })
  },
  methods: {
    formatTableData(tableData) {
      tableData.forEach(el => {
        el.category = this.categoryList.find(el2 => el.category_id === el2.id)
        el.count = el.CourseStudents ? el.CourseStudents.length : 0
      })
    },
    onEdit(row) {
      this.$confirm('确认选课？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          http.course_student.create(undefined, {
            student_id: this.userInfo.id,
            course_start_id: row.id,
            course_id: row.course_id,
            category_id: row.category_id,
            teacher_id: row.teacher_id
          }).then(res => {
            this.$message.success('选课成功')
            this.initData()
          })
        })
        .catch(() => {

        })
    }
  }
}
</script>

<style scoped lang="scss">
.common-page {
  height:  100%;
}
</style>
